package service

import (
	"errors"
	"gorm.io/gorm"
	"main/app"
	"main/internal/model"
	"main/internal/pkg/code"
)

var SysUserLiveListService = sysUserLiveListService{}

// sysUserLiveListService 业务层
type sysUserLiveListService struct {
}

func (t sysUserLiveListService) db() *gorm.DB {
	return app.Db
}

// Create 创建记录
func (t sysUserLiveListService) Create(v model.SysUserLiveList, uid uint) (model.SysUserLiveList, error) {
	v.CreateBy = uid
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Create(&v)
	if dbres.Error != nil {
		return model.SysUserLiveList{}, dbres.Error
	}
	return v, nil
}

// CreateList 批量创建记录
func (t sysUserLiveListService) CreateList(v []model.SysUserLiveList, uid uint) ([]model.SysUserLiveList, error) {
	for k, _ := range v {
		//v[k].AdminExpiration = time.Now().AddDate(10, 0, 0)
		v[k].CreateBy = uid
	}
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Create(&v)
	if dbres.Error != nil {
		return []model.SysUserLiveList{}, dbres.Error
	}
	return v, nil
}

// Delete 根据ID删除记录
func (t sysUserLiveListService) Delete(v uint, uid uint) (uint, error) {
	t.db().Table(model.SysUserLiveList{}.TableName()).Where("id = ?", v).Update("delete_by", uid)
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Where("id = ?", v).Delete(&v)
	if dbres.Error != nil {
		return v, dbres.Error
	}
	return v, nil
}

// DeleteList 根据ID删除记录
func (t sysUserLiveListService) DeleteList(v []uint, uid uint) ([]uint, error) {
	t.db().Table(model.SysUserLiveList{}.TableName()).Where("id IN (?)", v).Update("delete_by", uid)
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Where("id IN (?)", v).Delete(&v)
	if dbres.Error != nil {
		return v, dbres.Error
	}
	return v, nil
}

// Get 根据ID查询
func (t sysUserLiveListService) Get(v uint, lang string) (model.SysUserLiveList, error) {
	var data model.SysUserLiveList
	var count int64
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Where("id = ?", v).Find(&data).Count(&count)
	if count == 0 {
		err := errors.New(code.GetMsg(code.RecordNotExist, lang))
		return model.SysUserLiveList{}, err
	}
	if dbres.Error != nil {
		return data, dbres.Error
	}
	return data, nil
}

// GetList 批量查询
func (t sysUserLiveListService) GetList(page, size int, kw string, ids []uint, id uint, dateForm, dateTo, status, lang string) ([]model.SysUserLiveList, error, int64) {
	var data []model.SysUserLiveList
	var count int64
	limit := size
	offset := size * (page - 1)
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Count(&count)
	dbres = t.db().Table(model.SysUserLiveList{}.TableName())
	if kw != "" {
		dbres = dbres.Where("admin_email LIKE ?", "%"+kw+"%")
	}
	if id != 0 {
		dbres = dbres.Where("id = ?", id)
	}
	if len(ids) > 0 {
		if ids[0] != 0 {
			dbres = dbres.Where("id IN (?)", ids)
		}
	}
	if dateForm != "" {
		dbres = dbres.Where("created_at >= ?", dateForm)
	}
	if dateTo != "" {
		dbres = dbres.Where("created_at <= ?", dateTo)
	}
	if status != "" {
		dbres = dbres.Where("status = ?", status)
	}
	if page != 0 {
		dbres = dbres.Limit(limit).Offset(offset)
	}
	dbres.Find(&data).Count(&count)
	if count == 0 {
		return []model.SysUserLiveList{}, nil, count
	}
	if dbres.Error != nil {
		return data, dbres.Error, count
	}
	return data, nil, count
}

// UpdateData 根据ID更新数据
func (t sysUserLiveListService) UpdateData(v model.SysUserLiveList, uid uint) (model.SysUserLiveList, error) {
	var data model.SysUserLiveList
	v.UpdateBy = uid
	dbres := t.db().Table(model.SysUserLiveList{}.TableName()).Save(&v)
	if dbres.Error != nil {
		return data, dbres.Error
	}
	return data, nil
}
